package com.example.zkweixiu.app;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;

import com.example.zkweixiu.app.fragment.HomeFragment1;
import com.example.zkweixiu.app.fragment.HomeFragment2;
import com.example.zkweixiu.app.fragment.HomeFragment3;
import com.example.zkweixiu.app.fragment.HomeFragment4;
import com.example.zkweixiu.app.service.MyService;
import com.example.zkweixiu.app.service.MyService2;
import com.next.easynavigation.view.EasyNavigationBar;
import com.yanzhenjie.sofia.Sofia;

import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private static EasyNavigationBar navigationBar;

    private static final String[] tabText = {"工作台", "设备", "团队","我的"};

    private long mBackPressed;
    private static final int TIME_INTERVAL = 2000;
    //未选中icon
    private static int[] normalIcon = {R.mipmap.work01, R.mipmap.device2, R.mipmap.team2,R.mipmap.my2};
    //选中时icon
    private static int[] selectIcon = {R.mipmap.workbench1, R.mipmap.device1, R.mipmap.team1,R.mipmap.my1};

    private static List<Fragment> fragments = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Sofia.with(MainActivity.this).statusBarDarkFont().statusBarBackground(Color.parseColor("#FFFFFF"));
        navigationBar = findViewById(R.id.navigationBar);

        fragments.add(new HomeFragment1());
        fragments.add(new HomeFragment2());
        fragments.add(new HomeFragment3());
        fragments.add(new HomeFragment4());

        navigationBar.titleItems(tabText)
                .normalIconItems(normalIcon)
                .selectIconItems(selectIcon)
                .fragmentList(fragments)
                .fragmentManager(getSupportFragmentManager())
                .canScroll(false) //是否左右滑动
                .build();


        navigationBar.selectTab(0);
       // SQLiteDatabase db = LitePal.getDatabase();

        if (!isServiceRunning(this,"com.example.zkweixiu.app.service.MyService2")){
            Log.e("MyService","死去了");
            startService(new Intent(this, MyService2.class));
        }else {
            Log.e("MyService","活着");
        }
    }



    public  boolean isServiceRunning(Context context, String serviceName) {
        ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningServiceInfo> serviceInfoList = manager.getRunningServices(200);
        if (serviceInfoList.size() <= 0) {
            return false;
        }
        for (ActivityManager.RunningServiceInfo info : serviceInfoList) {
            if (info.service.getClassName().equals(serviceName)) {
                return true;
            }
        }
        return false;
    }
}
